{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from numpy import *\n",
    "from time import sleep\n",
    "import matplotlib\n",
    "from matplotlib import pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def loadfile(filename):\n",
    "    fr=open(filename)\n",
    "    data=[]\n",
    "    for line in fr.readlines():\n",
    "        t=list(map(float,line.strip().split('\\t')))\n",
    "#         print(t)\n",
    "        data.append(t)\n",
    "#     print(data)\n",
    "    return array(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "data=loadfile('testSet.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAD4CAYAAADxeG0DAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8li6FKAAAVKElEQVR4nO3db4hsd33H8c93751Fh5sg7r0gmOyMD/rAoIVylyD4oKJVog2GQh9oxyDkwcJVIVJFtAN9UoYigvVBLbK0keAMSKmKRRSr1NInbXGvJmIaLUFm1miLG/MggQ1cw3z7YHeT3dkzs2dmzjm/3++c9wsO3J27O/ObmXM+85vfX3N3AQDStRG6AACA9RDkAJA4ghwAEkeQA0DiCHIASNzVEA96/fp173a7IR4aAJJ1+/bt59z9xuztQYK82+1qf38/xEMDQLLMbJJ1O00rAJA4ghwAEkeQA0DiCHIASBxBDgCJI8gBIHEEOQBJ0mg0Urfb1cbGhrrdrkajUegiISeCHIBGo5F2d3c1mUzk7ppMJtrd3a1VmNf5g8pCrEe+s7PjTAgC4tHtdjWZXJxr0ul0NB6Pqy9Q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VDznu8pM/uwpAclvcvrN0HhWUn3nvn5Hkm/DlSW0plZS8chPnL3r4cuT8neLun9ZvY+Sa+RdLeZDd39Q4HLlRsTgipgZmNJO+4e+8pqKzOzByR9XtIfuvth6PIUzcyu6rgT912SfiXph5L+zN2fClqwEthx7eNxSc+7+8dDl6dKJzXyT7r7g6HLsgzayFGUv5V0l6TvmdkTZval0AUq0klH7sckfVfHnX//WMcQP/F2SQ9LeufJe/nESW0VkaJGDgCJo0YOAIkjyAEgcQQ5ACSOIAeAxBHkAJA4ghwAEkeQA0Di/h9rngaESpihGQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "def format_pic(data):\n",
    "    plt.scatter(data[:,0],data[:,1],color='k')\n",
    "    plt.show()\n",
    "format_pic(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.123105625617661"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def distanceIc(vecA,vecB):\n",
    "    return sqrt(sum(power(vecA - vecB, 2)))\n",
    "distanceIc(array([8,5]),array([4,6]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-0.87432032 -2.54821357]\n"
     ]
    }
   ],
   "source": [
    "#随机产生质心\n",
    "def randcent(data,k):\n",
    "    m,n=data.shape\n",
    "    cents=mat(zeros((k,n)))\n",
    "    for i in range(n):\n",
    "        minJ=min(data[:,i])\n",
    "        maxJ=max(data[:,i])\n",
    "        rangesJ=maxJ-minJ\n",
    "#         print(minJ,maxJ)\n",
    "        cents[:,i]=mat(minJ+rangesJ*random.rand(k,1))\n",
    "    return cents.A\n",
    "a=randcent(data,2)\n",
    "print(a[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 1.658985,  4.285136],\n",
       "        [-3.453687,  3.424321],\n",
       "        [ 4.838138, -1.151539],\n",
       "        [-5.379713, -3.362104],\n",
       "        [ 0.972564,  2.924086],\n",
       "        [-3.567919,  1.531611],\n",
       "        [ 0.450614, -3.302219],\n",
       "        [-3.487105, -1.724432],\n",
       "        [ 2.668759,  1.594842],\n",
       "        [-3.156485,  3.191137],\n",
       "        [ 3.165506, -3.999838],\n",
       "        [-2.786837, -3.099354],\n",
       "        [ 4.208187,  2.984927],\n",
       "        [-2.123337,  2.943366],\n",
       "        [ 0.704199, -0.479481],\n",
       "        [-0.39237 , -3.963704],\n",
       "        [ 2.831667,  1.574018],\n",
       "        [-0.790153,  3.343144],\n",
       "        [ 2.943496, -3.357075],\n",
       "        [-3.195883, -2.283926],\n",
       "        [ 2.336445,  2.875106],\n",
       "        [-1.786345,  2.554248],\n",
       "        [ 2.190101, -1.90602 ],\n",
       "        [-3.403367, -2.778288],\n",
       "        [ 1.778124,  3.880832],\n",
       "        [-1.688346,  2.230267],\n",
       "        [ 2.592976, -2.054368],\n",
       "        [-4.007257, -3.207066],\n",
       "        [ 2.257734,  3.387564],\n",
       "        [-2.679011,  0.785119],\n",
       "        [ 0.939512, -4.023563],\n",
       "        [-3.674424, -2.261084],\n",
       "        [ 2.046259,  2.735279],\n",
       "        [-3.18947 ,  1.780269],\n",
       "        [ 4.372646, -0.822248],\n",
       "        [-2.579316, -3.497576],\n",
       "        [ 1.889034,  5.1904  ],\n",
       "        [-0.798747,  2.185588],\n",
       "        [ 2.83652 , -2.658556],\n",
       "        [-3.837877, -3.253815],\n",
       "        [ 2.096701,  3.886007],\n",
       "        [-2.709034,  2.923887],\n",
       "        [ 3.367037, -3.184789],\n",
       "        [-2.121479, -4.232586],\n",
       "        [ 2.329546,  3.179764],\n",
       "        [-3.284816,  3.273099],\n",
       "        [ 3.091414, -3.815232],\n",
       "        [-3.762093, -2.432191],\n",
       "        [ 3.542056,  2.778832],\n",
       "        [-1.736822,  4.241041],\n",
       "        [ 2.127073, -2.98368 ],\n",
       "        [-4.323818, -3.938116],\n",
       "        [ 3.792121,  5.135768],\n",
       "        [-4.786473,  3.358547],\n",
       "        [ 2.624081, -3.260715],\n",
       "        [-4.009299, -2.978115],\n",
       "        [ 2.493525,  1.96371 ],\n",
       "        [-2.513661,  2.642162],\n",
       "        [ 1.864375, -3.176309],\n",
       "        [-3.171184, -3.572452],\n",
       "        [ 2.89422 ,  2.489128],\n",
       "        [-2.562539,  2.884438],\n",
       "        [ 3.491078, -3.947487],\n",
       "        [-2.565729, -2.012114],\n",
       "        [ 3.332948,  3.983102],\n",
       "        [-1.616805,  3.573188],\n",
       "        [ 2.280615, -2.559444],\n",
       "        [-2.651229, -3.103198],\n",
       "        [ 2.321395,  3.154987],\n",
       "        [-1.685703,  2.939697],\n",
       "        [ 3.031012, -3.620252],\n",
       "        [-4.599622, -2.185829],\n",
       "        [ 4.196223,  1.126677],\n",
       "        [-2.133863,  3.093686],\n",
       "        [ 4.668892, -2.562705],\n",
       "        [-2.793241, -2.149706],\n",
       "        [ 2.884105,  3.043438],\n",
       "        [-2.967647,  2.848696],\n",
       "        [ 4.479332, -1.764772],\n",
       "        [-4.905566, -2.91107 ]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[-3.18695357, -0.35938491],\n",
       "        [ 2.65077367, -2.79019029],\n",
       "        [ 1.98283629,  3.1465235 ]]),\n",
       " matrix([[ 2.        ,  1.40131808],\n",
       "         [ 0.        , 14.38757717],\n",
       "         [ 1.        ,  7.46974076],\n",
       "         [ 0.        , 13.82451582],\n",
       "         [ 2.        ,  1.07012854],\n",
       "         [ 0.        ,  3.72100021],\n",
       "         [ 1.        ,  5.10287596],\n",
       "         [ 0.        ,  1.95344443],\n",
       "         [ 2.        ,  2.87820544],\n",
       "         [ 0.        , 12.6071342 ],\n",
       "         [ 1.        ,  1.72819697],\n",
       "         [ 0.        ,  7.66752386],\n",
       "         [ 2.        ,  4.9782992 ],\n",
       "         [ 0.        , 12.03944381],\n",
       "         [ 1.        ,  9.12853034],\n",
       "         [ 1.        , 10.63785781],\n",
       "         [ 2.        ,  3.19328712],\n",
       "         [ 2.        ,  7.72812923],\n",
       "         [ 1.        ,  0.40704464],\n",
       "         [ 0.        ,  3.70393813],\n",
       "         [ 2.        ,  0.19870658],\n",
       "         [ 0.        , 10.45096113],\n",
       "         [ 1.        ,  0.9939764 ],\n",
       "         [ 0.        ,  5.89792691],\n",
       "         [ 2.        ,  0.5811161 ],\n",
       "         [ 0.        ,  8.95212169],\n",
       "         [ 1.        ,  0.54477501],\n",
       "         [ 0.        ,  8.78218528],\n",
       "         [ 2.        ,  0.13366927],\n",
       "         [ 0.        ,  1.56789487],\n",
       "         [ 1.        ,  4.44962474],\n",
       "         [ 0.        ,  3.85408683],\n",
       "         [ 2.        ,  0.17314448],\n",
       "         [ 0.        ,  4.57812521],\n",
       "         [ 1.        ,  6.83764117],\n",
       "         [ 0.        , 10.21746671],\n",
       "         [ 2.        ,  4.18623002],\n",
       "         [ 2.        ,  8.66060264],\n",
       "         [ 1.        ,  0.05182929],\n",
       "         [ 0.        ,  8.80142683],\n",
       "         [ 2.        ,  0.55980102],\n",
       "         [ 0.        , 11.00828158],\n",
       "         [ 1.        ,  0.66874131],\n",
       "         [ 0.        , 16.13692271],\n",
       "         [ 2.        ,  0.12131255],\n",
       "         [ 0.        , 13.20451644],\n",
       "         [ 1.        ,  1.24487442],\n",
       "         [ 0.        ,  4.62731043],\n",
       "         [ 2.        ,  2.56636314],\n",
       "         [ 2.        , 15.03382636],\n",
       "         [ 1.        ,  0.31170066],\n",
       "         [ 0.        , 14.09977691],\n",
       "         [ 2.        ,  7.23060484],\n",
       "         [ 0.        , 16.38148012],\n",
       "         [ 1.        ,  0.22210601],\n",
       "         [ 0.        ,  7.53399927],\n",
       "         [ 2.        ,  1.65985073],\n",
       "         [ 0.        ,  9.46260677],\n",
       "         [ 1.        ,  0.76751052],\n",
       "         [ 0.        , 10.32404878],\n",
       "         [ 2.        ,  1.26278911],\n",
       "         [ 0.        , 10.91228066],\n",
       "         [ 1.        ,  2.04544706],\n",
       "         [ 0.        ,  3.1174334 ],\n",
       "         [ 2.        ,  2.52266521],\n",
       "         [ 2.        , 13.13946002],\n",
       "         [ 1.        ,  0.19026129],\n",
       "         [ 0.        ,  7.81551107],\n",
       "         [ 2.        ,  0.11469363],\n",
       "         [ 0.        , 13.13769476],\n",
       "         [ 1.        ,  0.83358364],\n",
       "         [ 0.        ,  5.33153009],\n",
       "         [ 2.        ,  8.9788606 ],\n",
       "         [ 0.        , 13.03269849],\n",
       "         [ 1.        ,  4.12455116],\n",
       "         [ 0.        ,  3.36025918],\n",
       "         [ 2.        ,  0.8229119 ],\n",
       "         [ 0.        , 10.33987852],\n",
       "         [ 1.        ,  4.39510824],\n",
       "         [ 0.        ,  9.46472546]]))"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def Kmeans(data,k,ditancemeans=distanceIc,creatmeans=randcent):\n",
    "    #k表示质点\n",
    "    #m表示数据的个数\n",
    "    m,n=data.shape\n",
    "    changed=True\n",
    "    clus=mat(zeros((m,2)))\n",
    "#     print('cents:')\n",
    "    cents=creatmeans(array(data),k)\n",
    "#     print(cents)\n",
    "    zly=0\n",
    "    while changed:\n",
    "        changed=False\n",
    "        for i in range(m):\n",
    "            cent=-1\n",
    "            distance=inf\n",
    "            for j in range(k):\n",
    "#                 print('data,cents')\n",
    "#                 print(data[i],cents[j])\n",
    "                distan=ditancemeans(data[i],array(cents[j]))\n",
    "#                 print('distan',distan,'distance',distance)\n",
    "                if distan<distance:\n",
    "                    cent=j\n",
    "                    distance=distan\n",
    "#                     clus[i]=[cent,distance]\n",
    "                    zly=zly+1\n",
    "#             print('clus[i,0]',int(clus[i,0]),'cent',cent)\n",
    "            if int(clus[i,0])!=cent:\n",
    "                changed=True\n",
    "#                 print(cent,distance)\n",
    "            clus[i]=cent,distance**2\n",
    "        for i in range(k):#质点\n",
    "            \n",
    "            data_fit=data[nonzero(clus[:,0].A==i)[0]]\n",
    "            cents[i]=mean(data_fit,axis=0)\n",
    "#             print('data_index',data_fit,axis=0)\n",
    "#     print('zly:',zly)\n",
    "    #返回质心，分配结果\n",
    "    return cents, clus\n",
    "Kmeans(data,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2.80293085 -2.7315146 ]\n",
      " [-2.46154315  2.78737555]\n",
      " [ 2.6265299   3.10868015]\n",
      " [-3.38237045 -2.9473363 ]]\n"
     ]
    }
   ],
   "source": [
    "cents,clus=Kmeans(data,4)  \n",
    "print(cents)\n",
    "label_test=np.array(clus[:,0],dtype = np.int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([-2.7315146 ,  2.78737555,  3.10868015, -2.9473363 ])"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(len(cents))\n",
    "cents[:,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAD4CAYAAADxeG0DAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/A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KnCvxeq9ZJ2pd3I9pBkGdjs6QvSCr0zLK7P+HuI6Mf/kLSgjzbk4J3SXrF3Q+4+58lPSzpppzblAp3H3L33aP//AdVAq4n31alx8wWSFop6V/ybks7CPKUmdmNkgbd/fm825KxT0r6cd6NSFiPpNfHfHxIBQ63KjNbJGmJpP/OtyWpuk+VztbZvBvSDg5fToCZ/UTS3Dqful3SlyStyLZF6Znovbr7D0a/5nZVfjV/KMu2ZcDqXCv0b1lmdpGk70ta7+6/z7s9aTCzGyQddfddZrY07/a0gyBPgLu/r951M1ss6VJJz5uZVBlq2G1m73L3Ixk2MTGN3muVmd0i6QZJy714ixQOSVo45uMFkg7n1JbUmdk0VUL8IXd/NO/2pOgaSTea2d9Lmi7p7Wb2HXf/eM7tahoLgjJkZq9K6nP3kHdXa5uZXS/p65KudfdjebcnaWY2VZVJ3OWSBiX9UtLH3P2lXBuWAqv0PB6U9Ft3X593e7Iy2iP/vLvfkHdbWsEYOZL0DUlvk/SkmT1nZt/Ku0FJGp3I/bSkx1WZ/NtWxBAfdY2kT0haNvrv8rnRHisCRI8cACJHjxwAIkeQA0DkCHIAiBxBDgCRI8gBIHIEOQBEjiAHgMj9P7FP5TzK+QAwAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "def showdataSet(data,label_test,cents):\n",
    "    \n",
    "    label=unique(label_test)\n",
    "    label_format=len(label)\n",
    "    label_test=mat(label_test)\n",
    "    print(label[1])\n",
    "    dataSet={}\n",
    "    for i in range(label_format):\n",
    "        data_fit=data[nonzero(label_test[:,0].A==label[i])[0]]\n",
    "        dataSet['%d'%(label[i])]=data_fit\n",
    "#         dataSet['%d'%(format(label_format[i])]=data_fit\n",
    "#     print((dataSet))\n",
    "    for i in dataSet.keys():\n",
    "        plt.scatter(dataSet[i][:,0],dataSet[i][:,1])\n",
    "    cents=array(cents)\n",
    "    plt.scatter(cents[:,0],cents[:,1],marker='+',color='k',s=100)\n",
    "    plt.show()\n",
    "showdataSet(data,label_test,cents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(unique(label_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4, 20, 2)\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAD4CAYAAADxeG0DAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8li6FKAAAX5UlEQVR4nO3df4yV1Z3H8c9XYGUWW+jKEGDAxWZdo5FJcCdNU5NKoEW7iBLT0h9pY9JsyGbbFGhri7U1xHajibGIaZPG7HbXpHaFWqVLaaNWO5pt000HUNRSqmFFGYYFt4W2OLQMfPePOxdnLvfO3B/Pj3Oe5/1KCM4zM889d9TPc+ac7znH3F0AgHhdkHcDAACdIcgBIHIEOQBEjiAHgMgR5AAQual5vOjs2bN90aJFebw0AERr165db7h7d+31XIJ80aJFGhgYyOOlASBaZnaw3nWGVgAgcgQ5AESOIAeAyBHkABA5ghwAIkeQA0DkCHIAiBxBDgCRI8gBSJJ2HtipFY+sUO+DvVrxyArtPLAz7yahSbms7AQQlp0HdmrTzzfp1JlTkqShk0Pa9PNNkqSV71yZY8uSs/PATm3ZvUVHTh7R3Blzte7qdYV5b/TIAWjL7i3nQrzq1JlT2rJ7S04tSlb1QTV0ckguP/egKspvHQQ5AB05eaSl67Ep+oOKIAeguTPmtnQ9NkV/UBHkALTu6nWaPmX6uGvT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5TzK+QAwAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "showDataSet(data,label_test,cents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [],
   "source": [
    "#past\n",
    "def showDataSet(datamat,label_test,myCentroids):\n",
    "    \"\"\"\n",
    "    数据可视化\n",
    "    Parameters:\n",
    "        dataMat - 数据矩阵\n",
    "        labelMat - 数据标签\n",
    "        myCentroids - 质点\n",
    "    Returns:\n",
    "        无\n",
    "    \"\"\"\n",
    "    dataMat=np.array(datamat)\n",
    "    n = np.unique(label_test)\n",
    "    myCentroids_np=np.array(myCentroids)\n",
    "    last_01=[]\n",
    "    for j in range(len(n)):\n",
    "        tem=[]\n",
    "        for i in range(len(dataMat)):\n",
    "            if label_test[i]==j:\n",
    "                tem.append(dataMat[i])\n",
    "        last_01.append(tem)\n",
    "    last=np.array(last_01)\n",
    "    print((last).shape)\n",
    "    for i in range(len(last)):\n",
    "        plt.scatter(np.transpose(last[i])[0], np.transpose(last[i])[1])\n",
    "   \n",
    "    plt.scatter(np.transpose(myCentroids_np)[0], np.transpose(myCentroids_np)[1],marker='+',color='k',s=100)     \n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [],
   "source": [
    "#二分kmeans\n",
    "def bikmeans(data,k,ditancemeans=distanceIc):\n",
    "    m,n=data.shape\n",
    "    cluster=zeros((m,2))\n",
    "    cent0=mean(data,axis=0).tolist()\n",
    "#     print([cent0])\n",
    "    centlast=[cent0]\n",
    "#     print('tis')\n",
    "#     print(nonzero(cluster[:,0]==0))\n",
    "#   #计算每个点到质心的误差\n",
    "    for i in range(m):\n",
    "        cluster[i,1]=ditancemeans(cent0,data[i,:])**2\n",
    "    \n",
    "    \n",
    "    while len(centlast)<k:\n",
    "        lowinf=inf\n",
    "        for i in range(len(centlast)):\n",
    "            datasplit=data[nonzero(cluster[:,0]==i)[0],:]\n",
    "            cents, clus=Kmeans(datasplit,2)\n",
    "            splitSum=sum(clus)\n",
    "#             print('~~~~~')\n",
    "#             print(clus.shape)\n",
    "#             print(splitSum)\n",
    "            splitnotSum=sum(cluster[nonzero(cluster[:,0]!=i)[0],1])\n",
    "#             print(splitnotSum)\n",
    "            if lowinf>(splitSum+splitnotSum):\n",
    "                bestsplit=i\n",
    "                lowinf=splitSum+splitnotSum\n",
    "                cents_new=cents\n",
    "                clus_new=clus.copy()\n",
    "        clus_new[nonzero(clus_new[:,0]==1)[0],0]=len(centlast)\n",
    "        clus_new[nonzero(clus_new[:,0]==0)[0],0]=bestsplit\n",
    "        centlast[bestsplit]=cents_new[0,:].tolist()\n",
    "        centlast.append(cents_new[1,:].tolist())\n",
    "\n",
    "        \n",
    "        \n",
    "        \n",
    "        cluster[nonzero(cluster[:,0]==bestsplit)[0],:]= clus_new\n",
    "#         print('new',clus_new)\n",
    "    \n",
    "#       clusterAssment[nonzero(clusterAssment[:, 0].A == bestCentToSplit)[0], :] = bestClustAss\n",
    "#         lowinf=inf\n",
    "#         bestcluster=-1\n",
    "#         cents, clus=[],[]\n",
    "#         for i in range(len(centlast)):\n",
    "#             tempsplit=[]\n",
    "#             tempnotsplit=[]\n",
    "#             for j in range(m):\n",
    "#                 if int(cluster[j,0])==i:\n",
    "#                     tempsplit.append(data[j,:])\n",
    "#                 else:\n",
    "#                     tempnotsplit.append(cluster[j,:])\n",
    "# #             print('tempsplit',tempsplit)\n",
    "#             cents, clus=Kmeans(array(tempsplit),2)\n",
    "# #             print('cents',clus)\n",
    "#             splitSum=sum(clus)\n",
    "#             notsplitSum=sum(tempnotsplit)\n",
    "#             print(splitSum,notsplitSum)\n",
    "#             if lowinf>(splitSum+notsplitSum):\n",
    "#                 lowinf=splitSum+notsplitSum\n",
    "#                 bestcluter=i\n",
    "#                 splitcluster=clus.copy()\n",
    "#         print(bestcluter)\n",
    "#         centlast.extend((cents).tolist())\n",
    "# #         print('centslast')\n",
    "#         print(centlast)\n",
    "#         print('cents')\n",
    "#         #更新对应的簇\n",
    "# #         clusterAssment[nonzero(clusterAssment[:, 0].A == bestCentToSplit)[0], :] = bestClustAss\n",
    "#         cluster[nonzero(cluster[:, 0] == bestcluter)[0], :]=splitcluster\n",
    "    return  centlast,cluster"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [],
   "source": [
    "centlast,cluster=bikmeans(data,4)\n",
    "# bikmeans(data,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2.8791614782608703, 2.540654086956522], [-3.0953906153846154, -2.262190846153846], [-2.187999375, 3.0182478125000003], [2.7318916666666664, -2.904396733333334]]\n"
     ]
    }
   ],
   "source": [
    "label_test=np.array(cluster[:,0],dtype = np.int)\n",
    "print(centlast)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4,)\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAD4CAYAAADxeG0DAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8li6FKAAAZNklEQVR4nO3dYWxcV5UH8P+J49betNjQOBs7TjaJtso2pEGpRqjQVbFiSAqp2wp1I2BBrFgpX0CNsyVsQlVjBQGVIpG0Agl1l9Ui0VUbICSYsEpKi1sBW4STNG6zISQKhcR2lZTKhmZtajtnP4zHmRm/Gb+Zue/de9/7/6TI+Hkycycu550599x7RVVBRET+WmB7AEREVBsGciIizzGQExF5joGciMhzDORERJ5baONFFy9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zovILtvjiZKILBeRn4nIGRE5LSLbbY8paiJSJyInReTHtsdSKQbyiInIcgAfAvAH22OJ2LMA1qnqegC/BbDb8niMEpE6AN8E8GEAawF8XETW2h1VpKYAPKyqtwG4E8BnE/5+AWA7gDO2B1ENBvLo7QPwBQCJnlVW1WOqOjXz7UsAknYi83sBnFfVC6r6NoCnAdxveUyRUdURVT0x87//jGyAW2Z3VNERkXYAWwD8u+2xVIOBPEIich+AIVU9ZXssMfsMgP+2PQjDlgG4mPf9JSQ4sOUTkZUANgD4ld2RRGo/sgnXNdsDqQbP7KyRiPwUwNKAHz0C4IsANsU7ouiUe6+qenjmMY8g+7H8qTjHFgMJuJboT1kAICI3AfgBgG5V/ZPt8URBRO4FcFlVj4tIh+3xVIOBvEaq+sGg6yJyO4BVAE6JCJAtNZwQkfeq6usxDtGYUu81R0Q+DeBeAJ2avAUKlwAsz/u+HcCwpbHEQkTqkQ3iT6nqQdvjidBdAO4TkY8AaADwDhH5rqp+0vK4QuOCoJiIyGsAMqrq+u5qVRGRewB8HcAHVPWK7fGYJiILkZ3E7QQwBODXAD6hqqetDiwiks0+vgPgTVXttj2euMxk5J9X1Xttj6USrJGTKd8AcDOAZ0XkZRH5lu0BmTQzkfs5AEeRnfg7kNQgPuMuAJ8CsHHm9/nyTMZKDmJGTkTkOWbkRESeYyAnIvIcAzkRkecYyImIPMdATkTkOQZyIiLPMZATEXnu/wFxsNmc/5dNWAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "showDataSet(data,label_test,centlast)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
